package overall.manager
{
	
	import com.hurlant.crypto.rsa.RSAKey;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	public class RsaManager extends EventDispatcher
	{
		public function RsaManager()
		{
		}
		static private var _instance:RsaManager;
		static public function get instance():RsaManager{
			_instance=_instance||new RsaManager;
			return _instance;
		}
		
		
		private var _rsaLoader:URLLoader;
		private var _url:String="exponent.xml";
		private var _rsaObj:XML;
		public function start():void
		{
			_rsaLoader=new URLLoader;
			_rsaLoader.addEventListener(Event.COMPLETE,_loaderComplete);
			_rsaLoader.load(new URLRequest(_url));
		}
		private function _loaderComplete(e:Event):void
		{
			_rsaObj=XML(_rsaLoader.data);
			this.dispatchEvent(new Event(Event.COMPLETE))
		}
		
		public function getdemoStr(str:String):String{
			var byteArr:ByteArray=encrypt(str);
		    var str:String;
			for(var i:int=0;i<byteArr.length;i++){
				str+=int(byteArr[i]).toString(16)+",";
				trace(int(byteArr[i]).toString(16));
			}
			return str;
		}
		// 加密字符串  
		// 返回值:加密后的ByteArray  
		public function encrypt(info:String) : ByteArray {  
			var input : ByteArray = new ByteArray();  
			input.writeUTFBytes(info);  
			
			// 初始化RSAKey, 从flashvars中读取服务器生成的 modulus / exponent  
			var rsa : RSAKey = RSAKey.parsePublicKey("BB15A8B59B4FA6CF00CAD34DDA96EA7AE19ECBBEB533A97493E3896B4BC1E012609DDE5BF633F1400FB4F25154521E8C98F8D965EEDD355B687DCC1303E76AA36D5A6F8165EAEA8B4C26C15E10DDE4AC8CEA58085F82B97BDEF83E8703299F7A80397F27270E69C84D54646C25B3C3C08FB50397BBBD67C659571C14CEBDDCC5","010001");  
			
			// 加密  
			var dest : ByteArray = new ByteArray();  
			rsa.encrypt( input, dest, input.length, null);  
	
			return dest;  
		}  
	}
}